package com.hhkj.study.admin.user.service.biz.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.hhkj.study.admin.user.entity.UserWechatCompany;
import com.hhkj.study.admin.user.excel.UserWechatCompanyExcelRes;
import com.hhkj.study.admin.user.model.podo.UserWechatCompanyDo;
import com.hhkj.study.admin.user.model.podo.UserWechatCompanyPo;
import com.hhkj.study.admin.user.model.req.UserWechatCompanyReq;
import com.hhkj.study.admin.user.model.res.UserWechatCompanyRes;
import com.hhkj.study.admin.user.service.base.UserWechatCompanyService;
import com.hhkj.study.admin.user.service.biz.UserWechatCompanyBizService;
import com.hhkj.study.admin.user.service.extend.UserWechatCompanyExtendService;
import com.hhkj.study.common.cache.GlobalCache;
import com.ruoyi.common.base.biz.service.biz.BaseBizServiceImpl;
import com.ruoyi.common.enums.DictCodeEnum;
import com.ruoyi.common.utils.excl.utils.ExcelUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
* UserWechatCompanyBizServiceImpl
*
* @author SERVER
* @since 2024-09-06
*/
@Service
public class UserWechatCompanyBizServiceImpl extends BaseBizServiceImpl<UserWechatCompany, UserWechatCompanyReq, UserWechatCompanyRes, UserWechatCompanyDo, UserWechatCompanyPo, UserWechatCompanyService, UserWechatCompanyExtendService> implements UserWechatCompanyBizService {

    @Resource
    private GlobalCache globalCache;

    @Override
    public void export(UserWechatCompanyReq req, HttpServletResponse response) {
        List<UserWechatCompanyRes> wechatCompanyResList = list(req);
        if (CollectionUtil.isEmpty(wechatCompanyResList)) {
            return;
        }
        List<UserWechatCompanyExcelRes> userWeComExcelList = BeanUtil.copyToList(wechatCompanyResList, UserWechatCompanyExcelRes.class);
        ExcelUtils.exportExcel(userWeComExcelList, "用户企业微信信息", "用户企业微信信息", UserWechatCompanyExcelRes.class, "用户企业微信信息.xlsx", response);
    }

    @Override
    protected void afterPage(List<UserWechatCompanyRes> userWecomList) {
        assignRes(userWecomList);
    }

    @Override
    protected void afterList(List<UserWechatCompanyRes> userWecomList) {
        assignRes(userWecomList);
    }

    /**
     * 处理需要展示的字段
     * @param userWecomList
     */
    private void assignRes(List<UserWechatCompanyRes> userWecomList) {
        // 获取所有的字典表 id name形式
        Map<Long, String> dictMap = globalCache.cacheDictMap(Arrays.asList(DictCodeEnum.COURSE_TYPE.getCode(), DictCodeEnum.WECHAT_COMPANY_SOURCE.getCode()));
        userWecomList.forEach(userWeCom -> {
            userWeCom.setAddSourceValue(dictMap.get(userWeCom.getAddSource()));
            userWeCom.setIntentionalExam(dictMap.get(userWeCom.getIntentionalExamId()));
        });
    }
}